// pages/autolog/autolog.js
const app = getApp();
let tool = require('../../utils/tool.js');
const httpUtils = require('../../utils/httpUtils.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
  
    wx.getSetting({
      success(res) {
        console.log("getSetting:res:"+res.userInfo)
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            
            success: function (res) {
              that.setUserInfo(res);
            }
          })
        }
      }
    });

    wx.login({
      success: function (res) {
        let code = res.code;
        if (code) {
          app.globalData.code = code;
          //获取openid
          httpUtils.getOpenId({
            code: code,
            type: 9,
          }).then((res) => {
            //用openid登录
            let openid = res.data;
            app.globalData.openid = res.data;
            that.loginByOpenid(openid);
          });

        } else {
          wx.switchTab({
            url: '/pages/index/index',
          })
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }

    })
  },
  /**
   * 通过openid登录
   */
  loginByOpenid(openid) {
    let that = this;
    let paramArr = {
      openid: openid,
    }

    let rs = tool.getSign(paramArr, app.globalData.key); //返回 bodyParam heaer sign
    let apiUrl = app.globalData.apiURL + "api/Shop/loginbyopenid?sign=" + rs.sign;
    console.log("apiurl:", apiUrl)
    // console.log(rs);
    console.log("data:" + JSON.stringify(rs.bodyParam))
    let takesPro = new Promise(
      function (resolve, reject) {
        wx.request({
          url: apiUrl,
          header: rs.header,
          data: rs.bodyParam,
          method: 'post',
          success(res) {
            resolve(res);
          },
          complete: function () {
            wx.hideLoading();
          }
        })
      }
    );

    takesPro.then(function (res) {
      console.log("autologin返回" + JSON.stringify(res))
      if (res.data.code == 1) { //登录成功设置全局参数 跳转首页
        if(res.data.data){
          if( res.data.data.access_token){
            app.globalData.$TOKEN = res.data.data.access_token;
          }
          
          app.globalData.userDetail = res.data.data;
        }
        wx.switchTab({
          url: '/pages/index/index',
        })

      } else {
        wx.switchTab({
          url: '/pages/index/index',
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 设置用户信息
   */
  setUserInfo: function (res) {
    this.setData({
      userInfo: res.userInfo
    })
    app.globalData.userInfo = this.data.userInfo;
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})